package com.yx.accountBook.account.controller;

import com.yx.accountBook.account.domain.Account;
import com.yx.accountBook.account.service.AccountService;
import com.yx.accountBook.common.BaseController;
import com.yx.accountBook.common.ConsHint;
import com.yx.accountBook.common.result.JsonListResult;
import com.yx.accountBook.common.result.JsonPageResult;
import com.yx.accountBook.common.result.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.text.MessageFormat;

/**
 * @author cuihao
 * @create 2017-11-24-13:53
 */
@RestController
@RequestMapping(value = "/account")
public class AccountController extends BaseController {
    private final static Logger log = LoggerFactory.getLogger(AccountController.class);

    @Autowired
    private AccountService accountService;

    /**
     * 账单管理列表
     * @param type  类型: 0 全部 1：吃饭 2：油费 3：4:娱乐 5其他
     * @param minMoney 最小金额
     * @param maxMoney 最大金额
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JsonPageResult getCourseList(@RequestParam(value = "type", defaultValue = "0", required = false) Integer type,
                                        @RequestParam(value = "minMoney", defaultValue = "0", required = false) double minMoney,
                                        @RequestParam(value = "maxMoney", defaultValue = "0", required = false) double maxMoney,
                                        @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                        @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
//        Long creator = CurrentUser.getUser().getId();//当前登录人ID

        return accountService.getCourseList(type, minMoney, maxMoney,pageNo, pageSize);
    }

    /**
     * 删除账单
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public JsonResult deleteCourseById(@RequestParam(value = "id") long id){
        Assert.isTrue(0 < id, MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, "账单主键"));
        return accountService.deleteById(id);
    }

    /**
     * 获取账单明细
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public JsonResult getCourseDetailById(@RequestParam(value = "id") long id){
        Assert.isTrue(0 < id, MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, "账单主键"));
        return accountService.getDetailById(id);
    }

    /**
     * 新增账单
     * @param account
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JsonResult addAccount(@RequestBody Account account){
        Assert.notNull(account.getMoney(), MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, "账单金额"));
        Assert.notNull(account.getType(), MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, "账单类型"));
        Assert.notNull(account.getInfo(), MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, "账单描述"));
        return accountService.addAccount(account);
    }

    /**
     * 编辑账单
     * @param account
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    public JsonResult editAccount(@RequestBody Account account){
        Assert.notNull(account.getId(), MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, "账单id"));
        Assert.notNull(account.getMoney(), MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, "账单金额"));
        Assert.notNull(account.getType(), MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, "账单类型"));
        Assert.notNull(account.getInfo(), MessageFormat.format(ConsHint.ARG_VALIDATION_ERR, "账单描述"));

        return accountService.editAccount(account);
    }


    /**
     * 统计账单
     * @param
     * @return
     */
    @RequestMapping(value = "/count", method = RequestMethod.GET)
    public JsonListResult count(){

        return accountService.count();
    }

}
